﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouristAgencyDataLayer
{
    public class Tours
    {
        
        public static int CreateTour(int desId, DateTime start, DateTime end, int? mOfTranId, int? bId, string descr, decimal price)
        {
            if (!string.IsNullOrEmpty(descr) && start <= end)
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                var res =
                    (from de in context.Destinations
                     where de.DestinationId == desId
                     select de).FirstOrDefault();

                if (res != null)
                {
                    Tour b = new Tour()
                    {
                        BoardId = bId,
                        DestinationId = desId,
                        StartDate = start,
                        EndDate = end,
                        MeanOfTransportId = mOfTranId,
                        Description = descr,
                        Price = price,
                        RefNum = ((res.Destination1.Length >= 11) ? res.Destination1.Substring(0, 10) : res.Destination1) + start.ToString()
                    };

                    context.Tours.InsertOnSubmit(b);
                    context.SubmitChanges();

                    return b.TourId;
                }
            }
            return -1;
        }

        public static void UpdateTour(int tourId, int desId, DateTime start, DateTime end, int? mOfTranId, int? bId, string descr, decimal price)
        {
            if (!string.IsNullOrEmpty(descr) && start <= end)
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                var res =
                    (from b in context.Tours
                     where b.TourId == tourId
                     select b).FirstOrDefault();

                var res2 =
                    (from de in context.Destinations
                     where de.DestinationId == desId
                     select de).FirstOrDefault();

                if (res2 != null && res != null)
                {
                    res.DestinationId = desId;
                    res.StartDate = start;
                    res.EndDate = end;
                    res.MeanOfTransportId = mOfTranId;
                    res.BoardId = bId;
                    res.Description = descr;
                    res.Price = price;
                    res.RefNum = ((res2.Destination1.Length >= 11) ? res2.Destination1.Substring(0, 10) : res2.Destination1) + start.ToString();
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteTour(int tourId)
        {

            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var boa =
                (from b in context.Tours
                 where b.TourId == tourId
                 select b).FirstOrDefault();

            if (boa != null)
            {
                boa.Price = -1m;
                context.SubmitChanges();
            }
        }

        public static IQueryable<Tour> GetTours()
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();      

            return context.Tours.Where(t => t.Price >= 0);
        }

        public static Tour GetTourById(int id)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                (from b in context.Tours
                 where b.TourId == id
                 select b).FirstOrDefault();

            if (res != null)
            {
                return res;
            }
            else
            {
                throw new ArgumentException("Invalid tour id");
            }
        }
    }
}
